<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="项目类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="equipmentType">
              <j-dict-select-tag type="list" v-model="model.equipmentType" dictCode="equipment_type" placeholder="请选择项目类型" @change="typeChange"/>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="参数编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterCode">
              <a-input v-model="model.parameterCode" placeholder="请输入参数编码"  ></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="AKTICP系统变量ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterPlcId">
              <a-input v-model="model.parameterPlcId" placeholder="请输入AKTICP系统变量ID编码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="PLC变量所属一级组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterClass">
              <j-dict-select-tag type="list" v-model="model.parameterClass" dictCode="parameter_class" placeholder="请选择PLC变量所属一级组" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="PLC变量所属二级组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterControlObject">
                <j-category-select type="list" v-model="model.parameterControlObject" :pcode="pcode" placeholder="PLC变量所属二级组" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="PLC变量名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterName">
              <a-input v-model="model.parameterName" placeholder="请输入PLC变量名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="PLC变量地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paramAddress">
              <a-input v-model="model.paramAddress" placeholder="请输入PLC变量地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="PLC品牌型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="plcModel">
              <j-dict-select-tag type="list" v-model="model.plcModel" dictCode="plc_model" placeholder="请选择PLC品牌型号" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="PLC数据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterDataType">
              <j-dict-select-tag type="list" v-model="model.parameterDataType" dictCode="parameter_data_type" placeholder="请选择PLC数据类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="PLC范围" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterRange">
              <a-input v-model="model.parameterRange" placeholder="请输入PLC范围"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="PLC变量参考值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paramRefValue">
              <a-input v-model="model.paramRefValue" placeholder="请输入PLC变量参考值"  ></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="参数控制对象类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterControlObjectType">
              <j-dict-select-tag type="list" v-model="model.parameterControlObjectType" dictCode="parameter_control_object_type" placeholder="请选择参数控制对象类别" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="参数单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterUnit">
              <j-dict-select-tag type="list" v-model="model.parameterUnit" dictCode="yks_unit,name,id" placeholder="请选择参数单位" />
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="PLC读写类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parameterReadandwriteType">
              <j-dict-select-tag type="list" v-model="model.parameterReadandwriteType" dictCode="parameter_readandwrite_type" placeholder="请选择PLC读写类型" />
            </a-form-model-item>
          </a-col>

          <!-- <a-col :span="12">
            <a-form-model-item label="控件类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlType">
              <a-input v-model="model.controlType" placeholder="请输入控件类型"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="控件值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlValue">
              <a-input v-model="model.controlValue" placeholder="请输入控件值"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否必填" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlRequired">
               <j-dict-select-tag type="radio" v-model="model.controlRequired"  dictCode="yn" placeholder="请选择是否启用" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="备注"  :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea  v-model="model.remark" placeholder="请输入备注"  ></a-textarea >
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'YksPlcParameterForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          parameterControlObject:"",
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 14 },
        },
        confirmLoading: false,
        validatorRules: {
          equipmentType: [
              { required: true, message: '请输入项目类型!'},
           ],
           parameterClass: [
              { required: true, message: '请输入参数类别!'},
           ],
           parameterCode: [
              { required: true, message: '请输入参数编码!'},
           ],
           parameterName: [
              { required: true, message: '请输入参数名称!'},
           ],
           parameterPlcId: [
              { required: true, message: '请输入PLC参数ID!'},
           ],
           plcModel: [
              { required: true, message: '请输入品牌!'},
           ],
           parameterRange: [
              { required: true, message: '请输入参数取值范围!'},
           ],
           parameterUnit: [
              { required: true, message: '请输入参数单位!'},
           ],
           parameterDataType: [
              { required: true, message: '请输入参数数据类型!'},
           ],
           parameterReadandwriteType: [
              { required: true, message: '请输入参数读写数据类型!'},
           ],
          //  equipmentType: [
          //     { required: true, message: '请输入设备类型!'},
          //  ],
        },
        url: {
          add: "/mng/yksPlcParameter/add",
          edit: "/mng/yksPlcParameter/edit",
          queryById: "/mng/yksPlcParameter/queryById"
        },
        parameterClass:'',
        action:'',
        pcode:'z_',
      }
    },
    watch: {
      // pcode:{
      //   handler: function(nval,oval){
      //     this.pcode = this.model.equipmentType
      //   }
      // }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      typeChange(val){
          if (val) {
            this.pcode = val
          }            
          else {
            this.pcode = "z_"
          }            
          this.model.parameterControlObject = ""
      },
      add () {
        this.action='Add'
        this.edit(this.modelDefault);

      },
      edit (record) {
        
        this.model = Object.assign({}, record);
        if(this.action=='Add'){
          this.model.parameterClass=this.parameterClass
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>